var countLargestGroup = function (n) {
  let maxVal = 1;
  let maxCount = 0;
  let resMap = new Map();
  for (let i = 1; i <= n; i++) {
    let j = calculateDigitSum(i);
    if (resMap.has(j)) {
      resMap.get(j).push(i)
      if (maxVal === resMap.get(j).length) {
        maxCount++;
      }
      if (resMap.get(j).length > maxVal) {
        maxVal = resMap.get(j).length;
        maxCount = 1;
      }
    } else {
      resMap.set(j, [i]);
      if(maxVal === 1) {
        maxCount++;
      }
    }
  }
  return maxCount;
};

// 计算数位和
let calculateDigitSum = (num) => {
  let digitSum = 0;
  const numStr = Math.abs(num).toString(); // 转换为字符串

  for (let i = 0; i < numStr.length; i++) {
    digitSum += parseInt(numStr[i]); // 将字符转换为数字并相加
  }

  return digitSum;
};
console.log("result", countLargestGroup(2));
